package summary;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author: 海琳琦
 * @Date: 2022/4/17 17:46
 * https://leetcode-cn.com/problems/flood-fill/
 */
public class Title733 {

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int n = image.length;
        int m = image[0].length;
        int value = image[sr][sc];
        if (value == newColor) {
            return image;
        }
        image[sr][sc] = newColor;
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{sr, sc});
        //上 下 左 右
        int[] dx = {-1, 1, 0, 0};
        int[] dy = {0, 0, -1, 1};
        while (!queue.isEmpty()) {
            int[] poll = queue.poll();
            int x = poll[0];
            int y = poll[1];
            for (int i = 0; i < 4; i++) {
                int newX = x + dx[i];
                int newY = y + dy[i];
                if (newX >= 0 && newX < n && newY >= 0 && newY < m && image[newX][newY] == value) {
                    image[newX][newY] = newColor;
                    queue.offer(new int[]{newX, newY});
                }
            }
        }
        return image;
    }

    public static void main(String[] args) {

    }
}
